'use strict';
app.controller('goodsManageCtrl', ['$scope', '$cookies', 'utilService', '$modal', '$stateParams',
    function ($scope, $cookies, utilService, $modal, $stateParams) {
    //定义联想查询
    //$scope.states = [];
    $scope.goodsStatusObject = [
        {name: '下架', value: '0'},
        {name: '上架', value: '1'},
        {name: '待审核', value: '2'},
        {name: '审核不通过', value: '3'},
        {name: '审核通过', value: '4'}
    ]

    $scope.page = {};
    $scope.page.currentPage = 1;
    //分页
    $scope.replacePage = function () {
        $scope.currentPage = $scope.page.currentPage;
        $scope.pageChanged();
    };

    $scope.goods = {};
    $scope.type = 0;
    $scope.queryGoods = function () {
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + "goods/audit",
            params: $scope.goods
        }).then(function (result) {
        });
    }
    $scope.checkType = function (type) {
        if (type == 0) {
            $scope.list={};
            $scope.goods.isChain = 0;
            $scope.goods.isPlatform = 0;
            $scope.type = 0;
        }else if (type == 1) {
            $scope.list={};
            $scope.goods.isPlatform = 0;
            $scope.goods.isChain = 1;
            $scope.goods.status = 1;
            $scope.type = 1;
        }else if (type == 2) {
            $scope.list={};
            $scope.goods.isChain = 0;
            $scope.goods.isPlatform = 1;
            $scope.goods.status = 1;
            $scope.type = 2;
        }
        $scope.queryGoods();
    }
    $scope.checkType(0);


    //打开查看服务框
    $scope.checkReserve = function (id) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/goods/reserve_info.html',
            controller: 'reserveInfoCtrl',
            size: 'md',
            scope:$scope,
            resolve: {
                data: function () {
                    return id;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            $scope.selected = selectedItem;
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });
    };
}]);

app.controller('reserveInfoCtrl',['$scope','toaster','data','$modalInstance','goodsManageService',
    function($scope,toaster,data,$modalInstance,goodsManageService){
    $scope.id=data;
    $scope.goodsInfo={};

    $scope.checkReserve=function(){
        goodsManageService.checkReserve($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.goodsInfo=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    };

    $scope.checkReserve();

    $scope.ok = function(){
        $modalInstance.dismiss('cancel');
    };

}]);

//强制下架
app.controller('goodsManageSoldOutCtrl', ['$scope', '$stateParams', 'goodsManageService', 'toaster', '$state',
    function ($scope, $stateParams, goodsManageService, toaster, $state) {
        $scope.merchants = {
            id: $stateParams.id
        }
        $scope.changeStatus = function () {
            goodsManageService.changeStatus($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        history.back();
                        break;
                    case 673:
                        toaster.pop('error', "提示", "提示：该商品并未上架！");
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            });
        }
    }]);